#include"Manager_view_achieve.h"
#include "Library_view_achieve.h"
#include"Manager.h"
#include"Tool.h"
#include <string.h>
using namespace LB;
using namespace std;

Manager_view_achieve::Manager_view_achieve()
{
	init_id(MGID_PATH,1800);
	m_service=new Manager_service_achieve;
}
	
void Manager_view_achieve::menu(void)
{
	char namebuff[255] = {};
	for(int i = 0; i < 8;i++)
	{
		puts("");
	}
	cout<<"			   Input id：";
	cin >> namebuff;
	clear_stdin();
	cout << endl;
	char *passwd=getpass("			   Password: ");
	if(strcmp(namebuff,"admin") !=0 && strcmp(passwd,"admin") !=0)
	{
		return;
	}

	login_loading();
	while(1)
	{
		int choice = 0;
		system("clear");
		puts("	   ╭════════════════════════════════════════════════════╮");  
   		puts("	   │               　   超级管理员界面 　　　     　    │");  
		puts("	   ╰════■□■□══════════════════════════════════■□■□══════╯"); 
		puts(""); 
		puts("		  	     【1】增加管理员");
		puts("		  	     【2】删除管理员");
		puts("		  	     【3】管理员列表");
		puts("		  	     【4】返回登录界面");
		puts("");
		puts("	   ╭════════════════════════════════════════════════════╮"); 
		puts("	   ╰════■□■□══════════════════════════════════■□■□══════╯"); 
		puts("");
		cout << "	 	   	    	  请选择：";
		cin >> choice;
		clear_stdin();
		system("clear");
		switch(choice)
		{
			case 1: add();break;
			case 2: del();break;
			case 3:list();break;
			case 4: return ;
		}
		anykey2c();
		clear_stdin();
	}
}
	
bool Manager_view_achieve::login(void)
{
	int m_id;
	for(int i = 0; i < 8;i++)
	{
		puts("");
	}
	
	cout<<"			   Input id：";
	cin>>m_id;
	getchar();
	puts("");
	clear_stdin();
	char *passwd=getpass("			   Password: ");
	
	clear_stdin();
	if(m_service->loginManager(m_id,passwd)==1)
	{
		Library_view_achieve L;
		L.menu();
		return true;
	}
	else
	{
		cout<<"登录失败！"<<endl;
		return false;
	}
}
	
void Manager_view_achieve::add(void)
{
	Manager m;
	cout<<"请输入新增管理员姓名：";
	cin >> m.m_name;
	clear_stdin();
	cout << "请输入新增管理员密码: ";
	cin >> m.m_password;
	clear_stdin();

	m.m_id=	getid(MGID_PATH);
	m_service->addManager(m);
	cout << endl;
	cout<<"添加管理员成功！" <<endl;
	cout<<"管理员信息："<<endl;
	cout<<m<<endl;		
}
	
void Manager_view_achieve::del(void)
{
	cout<<"请输入要删除的管理员id："<<endl;
	int id;
	cin>>id;

	if(m_service->delManager(id)==1)
	{
		cout<<"删除管理员成功！"<<endl;
	}
	else
	{
		cout<<"删除管理员失败！"<<endl;
	}
	getchar();
}
	
void Manager_view_achieve::list(void)
{
	loading();
	cout<<"管理员列表为："<<endl;
	m_service->listManager();
}
	
Manager_view_achieve::~Manager_view_achieve( )
{
	delete m_service;
}